package cuongnh.project.japanesediary.vocabulary.ui.view.action.menucontribution.category.grammars;

import java.util.Set;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import cuongnh.project.japanesediary.vocabulary.model.Grammar;
import cuongnh.project.japanesediary.vocabulary.ui.view.CategoryGrammarsView;

public class RemoveCategoryGrammarAction implements IViewActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.action.RemoveCategoryGrammar";

	protected IViewPart m_view;

	private CommonServices commonServices;

	private Category category;

	private Set<Grammar> selectedGrammars;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	@Override
	public void init(IViewPart view) {
		m_view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		// 履歴ビュー更新
		CategoryGrammarsView view = (CategoryGrammarsView) m_view
				.getAdapter(CategoryGrammarsView.class);
		commonServices = view.getCommonServices();
		category = view.getCategory();
		selectedGrammars = view.getSelectedGrammars();
		if (!selectedGrammars.isEmpty()) {
			boolean confirm = MessageDialog
					.openConfirm(view.getSite().getShell(),
							"Deleting confirmation!",
							"Are you sure you want to delete this Grammar from current Category?");
			if (confirm) {
				// remove selected vocabulary to current category
				Category currentCategory = commonServices.getCategoryDao()
						.getCategoryWithGrammars(category.getCategoryId());
				currentCategory.getGrammars().removeAll(selectedGrammars);
				commonServices.getCategoryDao().update(currentCategory);
				view.update();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
	 * .IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
